package com.utilviet.baovn.Utility;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.utilviet.baovn.R;
import com.utilviet.baovn.define.AnimateViewType;

import java.io.File;

/**
 * Created by leanhtai on 6/20/15.
 */
public class Utility {
    public static int getDP(Context context, int value) {
        return (int)(value * context.getResources().getDisplayMetrics().density + 0.5f);
    }

    public static Animation doAnimation(Activity parentActivity, View view, AnimateViewType animateViewType, long durationMillis) {
        if (parentActivity == null || view == null) {
            return null;
        }

        Animation animation = null;

        if (animateViewType == AnimateViewType.RightIn) {
            animation = loadAnimation(parentActivity, R.anim.right_in, durationMillis);
        }
        else if (animateViewType == AnimateViewType.RightOut) {
            animation = loadAnimation(parentActivity, R.anim.right_out, durationMillis);
        }
        else if (animateViewType == AnimateViewType.LeftIn) {
            animation = loadAnimation(parentActivity, R.anim.left_in, durationMillis);
        }
        else if (animateViewType == AnimateViewType.LeftOut) {
            animation = loadAnimation(parentActivity, R.anim.left_out, durationMillis);
        }
        else if (animateViewType == AnimateViewType.BottomIn) {
            animation = loadAnimation(parentActivity, R.anim.bottom_in, durationMillis);
        }
        else if (animateViewType == AnimateViewType.BottomOut) {
            animation = loadAnimation(parentActivity, R.anim.bottom_out, durationMillis);
        }
        else if (animateViewType == AnimateViewType.FadeIn) {
            animation = loadAnimation(parentActivity, R.anim.fade_in, durationMillis);
        }
        else if (animateViewType == AnimateViewType.FadeIn60) {
            animation = loadAnimation(parentActivity, R.anim.fade_in_60, durationMillis);
        }
        else if (animateViewType == AnimateViewType.FadeOut) {
            animation = loadAnimation(parentActivity, R.anim.fade_out, durationMillis);
        }
        else if (animateViewType == AnimateViewType.ZoomIn) {
            animation = loadAnimation(parentActivity, R.anim.zoom_in, durationMillis);
        }
        else if (animateViewType == AnimateViewType.ZoomOut) {
            animation = loadAnimation(parentActivity, R.anim.zoom_out, durationMillis);
        }

        if (animation != null) {
            view.startAnimation(animation);
        }

        return animation;
    }

    public static Animation loadAnimation(Activity parentActivity, int animateId, long durationMillis) {
        Animation animation = AnimationUtils.loadAnimation(parentActivity, animateId);

        if (durationMillis > 0) {
            animation.setDuration(durationMillis);
        }
        else {
// Using value default from xml
        }

        return animation;
    }

    public static File getAppFolder() {
        // sdcard
        File sdcard = Environment.getExternalStorageDirectory();

        if (sdcard.exists() == false) {
            return null;
        }

        // ENGAGEcx folder
        File appFolder = new File(sdcard.getAbsolutePath() + File.separator + "baovn");

        if (appFolder.exists() == false) {

            if (appFolder.mkdir() == false) {
                return null;
            }
        }

        return appFolder;
    }
}
